
var Orders = new Class({
    Implements: Options,
    options: {
        'tagid':''
    },
    initialize: function(opts) {
        this.setOptions(opts);

        this.panel = document.id(this.options.tagid);
        if(!this.panel) return;
        this.panel.empty();


        this.begDate = this.endDate = new Date().format('%Y-%m-%d');
        this.content = '';
        this.listData = '';
        this.curPage = 0;
        this.calendar = function(){};
        this.pager = function(){};

        this.createOrdersTable();

        this.ordersRequest = new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Loading...');
                new Loader().show();
            },
            onComplete: function(resp) {
                var data;
                try {
                    data = eval('(' + resp + ')');
                }
                catch(err) { alert('Error...' + err); return; }
                new Loader().hide();

                this.fillContent(data);
            }.bind(this)
        }).send('table=orders&beg=' + this.begDate + '&end=' + this.endDate  + '&nc=' + Date.now());

    },
    createOrdersTable: function() {
        this.content = new Element('div#content');
        this.listData = new Element('table#listData[rules=groups]');

        this.panel.adopt(
            new Element('div', {'id':'legend',
                'html': '<b>Legend:</b>&nbsp;<tt style="background-color:#7bcf7b;">&nbsp;import;&nbsp;</tt>&nbsp;&nbsp;&nbsp;' +
                        '<i><b>A</b> - cws, <b>B</b> - bizrate, <b>F</b> - froogle, <b>G</b> - pricegrabber,' +
                        '<b>H</b> - bing, <b>J</b> - cj, <b>M</b> - cwm, <b>N</b> - nexttag, <b>O</b> - onetime,' +
                        '<b>S</b> - shopping,<b>L</b> - links</i>'
            }),
            new Element('div.tl[html=<div class="tr"><div>&nbsp;</div></div>]'),
            this.content.adopt(this.listData),
            new Element('div.bl[html=<div class="br"><div>&nbsp;</div></div>]')
        )

        var calendar = new Element('td.left');
        var pager = new Element('td.right');
        new Element('table#pager').adopt(
            new Element('tbody').adopt(
                new Element('tr').adopt(
                    calendar,
                    pager
                )
            )
        ).inject(this.content, 'top');

        this.calendar = new Calendar({
            'tagid': calendar,
            onSelect: function(bedDate, endDate) {
                this.begDate = bedDate;
                this.endDate = endDate;
                this.curPage = 0;

                this.ordersRequest.send('table=orders&curPage=0&beg=' + this.begDate + '&end=' + this.endDate + '&nc=' + Date.now());
            }.bind(this)
        });

        this.pager = new Pager({
            'tagid': pager,
            onNewpage: function(currentPage) {
                this.curPage = currentPage;

                this.ordersRequest.send('table=orders&beg=' + this.begDate + '&end=' + this.endDate + '&curPage=' + this.curPage + '&nc=' + Date.now());
            }.bind(this)
        });

        this.createHead();
    },
    createHead: function() {
        // make colgroups and head
        this.listData.adopt(
            new Element('thead').adopt(
                new Element('tr').adopt(
                    new Element('th.brdCol.w15[html=&nbsp;]'),
                    new Element('th.brdCol.w80[text=Time]'),
                    new Element('th#srchead.brdCol.w80[text=No]').store('tbname','no'),
                    new Element('th.brdCol.w50[text=COD]'),
                    new Element('th.brdCol.w50[text=Wnty]'),
                    new Element('th.brdCol.w50[text=Ship Price]'),
                    new Element('th.brdCol.w50[text=Ship Method]'),
                    new Element('th.brdCol.w50[text=Tax]'),
                    new Element('th.brdCol.w50[text=Amount]')
                )
            ),
            new Element('tfoot').adopt(
                new Element('tr').adopt(
                    new Element('th.tdFull[colspan=9][html=&nbsp;]')
                )
            ),
            new Element('tbody')
        );

        new Suggest({'url': '/Admin/webstat/getFromMySQL',
            'tagid': this.options.tagid,
            'table': 'sugord',
            'minLength':1,
            onChoice: function(orderid) {

                this.ordersRequest.send('table=orders&orderid=' + orderid + '&nc=' + Date.now());
            }.bind(this)
        });

    },
    fillContent: function(data) {
        this.pager.setData(this.curPage, data.cntPage, data.onPage, data.rows.length);

        var tbody,order,tr,td,color=false,date,dparse;

        tbody = this.listData.getElement('tbody');
        tbody.empty();

        for(var i = 0; i < data.rows.length; i++) {
            order = data.rows[i];

            // set tr date
            dparse = Date.parse(order.date).format('%m%d%Y');
            if(date != dparse) {
                date = dparse;

                tbody.adopt(
                    new Element('tr').adopt(
                        new Element('td.trdate[colspan=9][text=' + Date.parse(order.date).format('%a %b %d %Y') + ']')
                    )
                )
            }

            tr = new Element('tr[style=background-color:#' + ((color = !color) ? 'ffffff' : 'fcffcd') + ';]',{
                'events': {
                    'mouseenter': function() {this.addClass('rowHover');},
                    'mouseleave': function() {this.removeClass('rowHover');}
                }
            });

            td = new Element('td.imgCol' + (order.impt == 'Y' ? '.bgreen' : '') + ']').adopt(
                new Element('a.open[href=javascript:;][html=&nbsp;][title=Open order]',{
                    'events': {
                        'click': this.getOrder.bind(this, order.id),
                        'focus': function() {this.blur();}
                    }
                })
            );

            tbody.adopt(
                tr.adopt(
                    td,
                    new Element('td.brdCol.center.w80[text=' + Date.parse(order.date).format('%H:%M:%S %a') + ']'),
                    new Element('td.brdCol.right.w80[text=' + order.no + ']'),
                    new Element('td.brdCol.center.w50[text=' + (order.cod == 'Y' ? 'Y' : '') + ']'),
                    new Element('td.brdCol.center.w50[text=' + (order.wty == 'Y' ? 'Y' : '') + ']'),
                    new Element('td.brdCol.right.w50[text=' + (order.sh_pr ? '$' + numberFormat(order.sh_pr) : '') + ']'),
                    new Element('td.brdCol.center.w50[text=' + order.sh_md + ']'),
                    new Element('td.brdCol.right.w50[text=' + (order.tax ? '$' + numberFormat(order.tax) : '') + ']'),
                    new Element('td.brdCol.right.w50[text=$' + numberFormat(order.amount) + ']')
                )
            );
        }// for
    },
    getOrder: function(orderid, ev) {
        ev.stop();
        new ShowOrder(orderid, ev.target);
    }
});

